package com.itheima.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Utils.MybaitsUtils;
import com.itheima.dao.DeptDao;
//import com.itheima.dao.DeptDaoImpl;
import com.itheima.domain.Dept;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class DeptServiceImpl implements DeptService {
   // DeptDaoImpl deptDaoImpl =  new DeptDaoImpl();
    @Override
    public List<Dept> selectAllDept() {
        //获取SqlSession对象
        SqlSession sqlSession = MybaitsUtils.getsqlSession();
        //获取deptDao的接口实现类对象
        DeptDao depeDao = sqlSession.getMapper(DeptDao.class);
        //调用实现类的方法来完成操作
        //分页,要写集合的前面
        PageHelper.startPage(2,3);

        List<Dept> listDept = depeDao.selectAllDept();
        //释放资源
        sqlSession.close();
        //返回结果
        return listDept;
       // return deptDaoImpl.selectAllDept();
    }

    @Override
    public Dept selectByDept(int deptno) {
        SqlSession sqlSession = MybaitsUtils.getsqlSession();

        DeptDao deptDao = sqlSession.getMapper(DeptDao.class);

        Dept dept = deptDao.selectByDept(deptno);

        sqlSession.close();

        return dept;
        //deptDaoImpl.selectByDept();
    }

    @Override
    public int insertDept(Dept dept) {
        SqlSession sqlSession = MybaitsUtils.getsqlSession();

        DeptDao deptDao = sqlSession.getMapper(DeptDao.class);

        int insert = deptDao.insertDept(dept);

        sqlSession.commit();

        sqlSession.close();

        return insert;
        //return deptDaoImpl.insertDept(dept);
    }

    @Override
    public int deleteDept(int deptno) {
        SqlSession sqlSession = MybaitsUtils.getsqlSession();

        DeptDao deptDao = sqlSession.getMapper(DeptDao.class);

        int delete = deptDao.deleteDept(deptno);

        sqlSession.commit();

        sqlSession.close();

        return delete;
        //return deptDaoImpl.deleteDept(deptno);
    }

    @Override
    public int updateDept(Dept dept) {
        SqlSession sqlSession = MybaitsUtils.getsqlSession();

        DeptDao deptDao = sqlSession.getMapper(DeptDao.class);

        int update = deptDao.updateDept(dept);

        sqlSession.commit();

        sqlSession.close();

        return update;
       // return deptDaoImpl.updateDept(dept);
    }
/*
* 根据编号和部门来查询
* */
    @Override
    public List<Dept> selectByNameOrAge(int deptno, String dname) {

        SqlSession sqlSession = MybaitsUtils.getsqlSession();

        DeptDao deptDao = sqlSession.getMapper(DeptDao.class);

        List<Dept> lists = deptDao. selectByNameOrAge(deptno,dname);

        sqlSession.commit();

        sqlSession.close();

        return lists;
    }

    @Override
    public List<Dept> selectCondition(Dept dept) {
        SqlSession sqlSession = MybaitsUtils.getsqlSession();

        DeptDao deptDao = sqlSession.getMapper(DeptDao.class);

        List<Dept> lists = deptDao.selectCondition(dept);

        sqlSession.commit();

        sqlSession.close();

        return lists;
    }

  /*  @Override
    public List<Dept> selectByIds(List list) {
        SqlSession sqlSession = MybaitsUtils.getsqlSession();

        DeptDao deptDao = sqlSession.getMapper(DeptDao.class);

        List<Dept> lists = deptDao.selectByIds(list);
        sqlSession.commit();
        sqlSession.close();
        return lists;
    }*/
}
